package p500;

import java.util.*;

public class Leetcode_560 {
    public int subarraySum(int[] nums, int k) {
        Map<Integer, Integer> map = new HashMap<>();
        map.put(0, 1);
        int sum = 0, res = 0;
        for (int i = 0; i < nums.length; i++) {
            sum += nums[i];
            if (map.containsKey(sum - k)) {
                res += map.get(sum - k);
            }
            map.put(sum, map.getOrDefault(sum, 0) + 1);
        }
        return res;
    }

    public static void main(String[] args) {
        Leetcode_560 l = new Leetcode_560();
        System.out.println(l.subarraySum(new int[]{1, 2, 3, 3, 2, 1, 2, 3, 4, 5, 6, 4, 3, 1}, 6));
    }

}
